package cc.mrbird.febs.cos.controller;

import cc.mrbird.febs.common.utils.AddressUtil;
import cc.mrbird.febs.common.utils.FileUtils;
import cc.mrbird.febs.common.utils.HttpContextUtil;
import cc.mrbird.febs.common.utils.IPUtil;
import cc.mrbird.febs.cos.entity.*;
import cc.mrbird.febs.cos.service.*;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/web")
@AllArgsConstructor
public class WebController {

    private final IAccessInfoService iAccessInfoService;

    private final SimpleDateFormat sdf2= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    private final ICarouselInfoService carouselInfoService;

    private final IEstateInfoService estateInfoService;

    private final IMessageInfoService messageInfoService;

    private final IBiddingInfoService biddingInfoService;

    private final IMailboxInfoService mailboxInfoService;

    private final IHouseTypeService iHouseTypeService;



    /**
     * 页面跳转
     * @param
     * @return
     */
    @RequestMapping("{page}")
    public String showPage(@PathVariable("page") String page) {
        HttpServletRequest request = HttpContextUtil.getHttpServletRequest();
        String ip = IPUtil.getIpAddr(request);
        iAccessInfoService.save(new AccessInfo(ip,AddressUtil.getCityInfo(ip),sdf2.format(new Date())));
        return page;
    }

    /**
     *
     * @param file 上传的文件
     * @return
     */
    @ResponseBody
    @RequestMapping("/fileUpload")
    public String upload(@RequestParam("avatar") MultipartFile file){
        //1定义要上传文件 的存放路径
        // /usr/project/estate/web/dist/static/img/
        // E:/project/房地产项目/frontend/static/img/image
        String localPath="E:/Project/项目交易平台（项目文件）/房地产项目/frontend/static/img/image";
//        String localPath = "/root/photo";
        //2获得文件名字
        String fileName=file.getOriginalFilename();
        //2上传失败提示
        String warning="";
        String newFileName = FileUtils.upload(file, localPath, fileName);
        MultipartFile file1 = file;
        // FileUtils.netUpload(file1,"E:/project/房地产项目/backend/src/main/resources/static/images/web",newFileName);
        if(newFileName != null){
            //上传成功
            warning=newFileName;

        }else{
            warning="上传失败";
        }
        System.out.println(warning);
        return warning;
    }

    @RequestMapping(value="index.html",method = RequestMethod.GET)
    public String goIndex(Model model) {
        Page page = new Page();
        page.setSize(3);
        model.addAttribute("imgList", carouselInfoService.page(page, Wrappers.<CarouselInfo>lambdaQuery()));

        Page page1 = new Page();
        page1.setSize(4);
        model.addAttribute("messList", messageInfoService.page(page1, Wrappers.<MessageInfo>lambdaQuery()));

        Page page2 = new Page();
        page2.setSize(6);
        model.addAttribute("estateList", estateInfoService.page(page2, Wrappers.<EstateInfo>lambdaQuery()));
        return "index";
    }

    @RequestMapping(value="estateInfo.html",method = RequestMethod.GET)
    public String goEstateInfo(@RequestParam Integer pageNo,@RequestParam Integer type, Model model) {
        Page pagehelper = new Page();
        pagehelper.setCurrent(pageNo);
        pagehelper.setSize(5);
        model.addAttribute("estateList", estateInfoService.page(pagehelper,Wrappers.<EstateInfo>lambdaQuery()
                .eq(type != -1, EstateInfo::getEstateType,type)));
        model.addAttribute("estateType",type);
        return "estateInfo";
    }

    @RequestMapping(value="estateShow.html",method = RequestMethod.GET)
    public String goEstateShow(@RequestParam Integer estateId, Model model) {
        model.addAttribute("estate", estateInfoService.getById(estateId));
        List<HouseType> houseTypes = iHouseTypeService.list(Wrappers.<HouseType>lambdaQuery().eq(HouseType::getEstateCode,estateId));
        model.addAttribute("houseTypes", houseTypes);
        return "estateShow";
    }

    @RequestMapping(value="biddingInfo.html",method = RequestMethod.GET)
    public String goBiddingInfo(@RequestParam Integer pageNo,@RequestParam Integer type, Model model) {
        Page pagehelper = new Page();
        pagehelper.setCurrent(pageNo);
        pagehelper.setSize(5);
        model.addAttribute("biddingList", biddingInfoService.page(pagehelper,Wrappers.<BiddingInfo>lambdaQuery()
                .isNull(type != -1, BiddingInfo::getCompanyName)
                .isNotNull(type == -1, BiddingInfo::getCompanyName)));
        model.addAttribute("biddingType",type);
        return "biddingInfo";
    }

    @RequestMapping(value="biddingShow.html",method = RequestMethod.GET)
    public String goBiddingShow(@RequestParam Integer id, Model model) {
        model.addAttribute("bidding", biddingInfoService.getById(id));
        return "biddingShow";
    }

    @RequestMapping(value="messageInfo.html",method = RequestMethod.GET)
    public String goMessageInfo(@RequestParam Integer pageNo, @RequestParam Integer type, Model model) {
        Page pagehelper = new Page();
        pagehelper.setCurrent(pageNo);
        pagehelper.setSize(5);
        model.addAttribute("messList", messageInfoService.page(pagehelper,Wrappers.<MessageInfo>lambdaQuery()
                .eq(type != null, MessageInfo::getMessType, type)));
        model.addAttribute("messType",type);
        return "messageInfo";
    }

    @RequestMapping(value="messageShow.html",method = RequestMethod.GET)
    public String goMessageShow(@RequestParam Integer id, Model model) {
        MessageInfo messageInfo = messageInfoService.getById(id);
        if(messageInfo != null) {
            messageInfoService.update(Wrappers.<MessageInfo>lambdaUpdate().set(MessageInfo::getAccessNum, messageInfo.getAccessNum() + 1).eq(MessageInfo::getId, id));
        }
        model.addAttribute("message", messageInfo);
        return "messageShow";
    }

    @ResponseBody
    @RequestMapping(value="sendMess",method = RequestMethod.POST)
    public void sendMess(@RequestParam("name") String name,@RequestParam("email") String email,@RequestParam("message") String message) {
        mailboxInfoService.save(new MailboxInfo(sdf2.format(new Date()),name, email, message));
    }
}
